package com.share51.novel.config;

import com.share51.novel.common.StatusConstant;
import com.share51.novel.job.BloomFilterJob;
import com.share51.novel.job.CrawlChapterJob;
import com.share51.novel.job.SyncNovelChapterJob;
import lombok.Getter;
import lombok.Setter;
import org.quartz.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 定时任务配置器
 * @author gb
 */
@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix = "com.share51.cron")
public class QuartzConfig {

    /**
     * 小说章节正文爬取定时任务 cron 表达式
     */
    private String crawlChapter;

    /**
     * 小说章节同步定时任务 cron 表达式
     */
    private String syncNovelChapter;

    /**
     * 每周初始化 布隆过滤器 cron 表达式
     */
    private String initBloomFilter;

    // CrawlChapterJob 配置

    @Bean("crawlChapterJobDetail")
    public JobDetail crawlChapterJobDetail(){
        return JobBuilder
                .newJob(CrawlChapterJob.class)
                .withIdentity("crawlChapter")
                .storeDurably()
                .build();
    }
    @Bean("crawlChapterTrigger")
    public Trigger crawlChapterTrigger(){
       return TriggerBuilder.newTrigger()
                .forJob(crawlChapterJobDetail())
                .withIdentity("crawlChapter")
                .withSchedule(CronScheduleBuilder.cronSchedule(crawlChapter))
                .usingJobData(StatusConstant.QUARTZ_RUNNING_STATUS,false)
                .build();
    }

    // SyncNovelChapterJob 配置

    @Bean("syncNovelChapterJobDetail")
    public JobDetail syncNovelChapterJobDetail(){
        return JobBuilder
                .newJob(SyncNovelChapterJob.class)
                .withIdentity("syncNovelChapter")
                .storeDurably()
                .build();
    }

    @Bean("syncNovelChapterTrigger")
    public Trigger syncNovelChapterTrigger(){
        return TriggerBuilder.newTrigger()
                .forJob(syncNovelChapterJobDetail())
                .withIdentity("syncNovelChapter")
                .withSchedule(CronScheduleBuilder.cronSchedule(syncNovelChapter))
                .usingJobData(StatusConstant.QUARTZ_RUNNING_STATUS,false)
                .build();
    }

    @Bean("initBloomFilterJobDetail")
    public JobDetail initBloomFilterJobDetail(){
        return JobBuilder
                .newJob(BloomFilterJob.class)
                .withIdentity("initBloomFilter")
                .storeDurably()
                .build();
    }

    @Bean("initBloomFilterTrigger")
    public Trigger initBloomFilterTrigger(){
        return TriggerBuilder.newTrigger()
                .forJob(initBloomFilterJobDetail())
                .withIdentity("initBloomFilter")
                .withSchedule(CronScheduleBuilder.cronSchedule(initBloomFilter))
                .usingJobData(StatusConstant.QUARTZ_RUNNING_STATUS,false)
                .build();
    }


}
